# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 3/19/2021 11:01
desc:
'''
import logging

from util.db_util import execute_query


def get_std_fin_item_id(name):
    """获取指定名称标准化科目的id
    Todo: 调用get_std_fin_item_info

    :param name: 科目名称

    :return: 科目id
    """
    results = execute_query("SELECT id FROM `t_das_std_fin_item` WHERE `name`='%s'" % name)
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


def get_std_fin_item_info(field_names_str,**kwargs):
    """获取标准化科目的信息

    :param field_names_str: 数据库字段名称字符串
    :param kwargs:
        | std_code：标准化编码
        | id：标准化科目id
        | group_name：科目分组名称
        | is_output：是否输出科目：0：否，1：是
        | has_Hierarchy：是否层级科目：0：否，1：是

    :return: 标准化科目的类型
    """
    condition = ""
    for kwarg in kwargs:
        if kwarg in ['id','acc','y','item_type','status','is_output','has_hierarchy','ref_model_id','ref_element_id']:
            condition += " and %s=%s" % (kwarg, kwargs[kwarg])
        else:
            condition += " and %s='%s'" % (kwarg, kwargs[kwarg])

    condition = condition.lstrip(" and")
    # if 'std_code' in kwargs:
    #     condition="std_code='%s'"%kwargs['std_code']
    # elif 'id' in kwargs:
    #     condition="id=%s"%kwargs['id']
    # elif 'has_Hierarchy' in kwargs:
    #     condition = "has_Hierarchy=%s" % kwargs['has_Hierarchy']
    # elif 'group_name' and 'is_output' in kwargs:
    #     condition = "status=0 and group_name='%s'and is_output=%s " % (kwargs['group_name'], kwargs['is_output'])
    # else:
    #     logging.error("get_std_fin_item_info:params required")
    #     return None
    sql = "select %s from t_das_std_fin_item where %s"%(field_names_str,condition)
    try:
        res = execute_query(sql)
        fields=field_names_str.split(',')
        if len(fields)==1:
            if len(res) > 1:
                result = [res[i][0] for i in range(len(res))]
            else:
                result = res[0][0]
        else:
            if len(res) > 1:
                result = [res[i][:len(fields)] for i in range(len(res))]
            else:
                result=list(res[0][:len(fields)])
        return result
    except Exception as e:
        logging.error("获取标准化科目的信息失败：%s，field_name_str %s kwargs %s"%(e,field_names_str,kwargs))
        return None


def get_std_tbl_def_id(name):
    """获取指定名称标准化数据表的id

    :param name: 标准数据表名称

    :return: 标准数据表id
    """
    results = execute_query("SELECT id FROM `t_das_std_data_tbl_def` WHERE `tbl_name`='%s'" % name)
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None


def get_std_unit_type():
    """获取标准字段单位分组类型

    :return:单位类型list
    """

    results = execute_query("SELECT DISTINCT unit_type FROM `t_das_std_unit`")
    try:
        unit_type = [results[i][0] for i in range(len(results))]
        return unit_type
    except Exception as e:
        logging.error("get_std_unit_type:%s" % e)
        return None


def get_std_unit(unit_type):
    """获取指定单位分组类型下单位列表

    :param unit_type: 单位分组类型

    :return: 单位名称list,单位编码list
    """

    results = execute_query(
        "SELECT unit_name,unit_code FROM `t_das_std_unit` WHERE unit_type='%s' ORDER BY id" % unit_type)
    try:
        unit_name = [results[i][0] for i in range(len(results))]
        unit_code = [results[i][1] for i in range(len(results))]
        return unit_name,unit_code
    except Exception as e:
        logging.error("get_std_unit:%s" % e)
        return None


def get_std_unit_id(unit_type,unit_name):
    """获取指定单位id

    :param unit_type: 单位分组类型
    :param unit_code: 单位编码

    :return: 单位id
    """

    results = execute_query(
        "SELECT id FROM `t_das_std_unit` WHERE unit_type='%s' AND unit_name='%s'" % (unit_type,unit_name))
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("get_std_unit_id:%s" % e)
        return None


def get_std_item_group_id(group_name):
    """获取指定科目分组的id

    :param group_name: 科目分组名称

    :return: 科目分组的id
    """
    results = execute_query(
        "SELECT id FROM `t_das_std_item_group` WHERE group_name='%s'" %group_name)
    try:
        result = results[0][0]
        return result
    except Exception as e:
        logging.error("no result retrieved")
        return None

